/*
 * File     : StructureByValueAsPointerParameter.java
 * Created  : 3 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.call;

import static com.googlecode.dni.internal.DniTypes.NATIVE_OBJECT_TYPE;
import static com.googlecode.dni.internal.DniTypes.POINTER_TYPE;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.internal.MemoryAccess;
import com.googlecode.dni.internal.structure.StructureRecord;
import com.googlecode.dni.type.structure.IllegalStructureException;
import com.googlecode.dni.type.structure.Structure;

/**
 * <p>
 *  Encapsulates a parameter to a native call that holds a structure passed by
 *  value, where a pointer to stack memory is passed.  Used by the Windows-style
 *  calling conventions.
 * </p>
 *
 * @author Matthew Wilson
 */
final class StructureByValueAsPointerParameter extends Parameter
{

    private final StructureRecord structureRecord;


    /**
     * Creates a new instance.
     *
     * @param parameter
     *            the source parameter
     */
    public StructureByValueAsPointerParameter( final StructureByValueParameter parameter )
    {
        Class< ? extends Structure > structType = parameter.getJavaType().asSubclass( Structure.class );
        this.structureRecord = DniInternal.getStructureRecord( structType );

        int size = this.structureRecord.getInfo().getSize();
        if ( size <= 0 )
        {
            throw new IllegalStructureException( "Cannot pass opaque structure by value: " + structType.getName() );
        }

        setNativeTypeAsPointer();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    int getJavaVarSlots()
    {
        return 1;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    boolean needsScratchMemory()
    {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeScratchMemorySize( final MethodVisitor methodVisitor )
    {
        DniInternal.writeConstantInt( methodVisitor, this.structureRecord.getInfo().getSize() );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writePrepareParameter( final MethodVisitor methodVisitor )
    {
        methodVisitor.visitVarInsn( ALOAD, getJavaVarIndex() );
        methodVisitor.visitMethodInsn( INVOKEINTERFACE,
                                       NATIVE_OBJECT_TYPE,
                                       "pointer",
                                       "()L" + POINTER_TYPE + ";" );

        writeGetScratchPointer64( methodVisitor );

        DniInternal.writeConstantInt( methodVisitor, this.structureRecord.getInfo().getSize() );
        methodVisitor.visitInsn( Opcodes.I2L );

        methodVisitor.visitMethodInsn( INVOKESTATIC,
                                       MemoryAccess.UNSAFE_ACCESS_TYPE,
                                       "copy",
                                       "(JJJ)V" );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeGetParameter( final MethodVisitor methodVisitor )
    {
        writeGetScratchPointer();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeReturnParameter( final MethodVisitor methodVisitor, final int returnVarIndex )
    {
        throw new UnsupportedOperationException( "Internal error: missing code to return structures by value" );
    }

}
